package com.elite.groovy.callback

import com.elite.groovy.util.EliteBean
import net.sf.json.JSONArray
import net.sf.json.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

public class CallProcedurePub implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(CallProcedurePub.class);
	public String doCallBack(EliteBean eliteBean,HttpServletRequest request,HttpServletResponse response) {

		response.setCharacterEncoding("UTF-8");
		log.debug("start GetDataInBound-----------------");
		log.debug("request--:"+request);

		String getMethod = request.getMethod();
		String callJson="";
		String procedureData="";
		String outData="";
		BufferedReader iny = null;
		List<String> list = new ArrayList<String>();

		def reJson = "";

		log.debug("getMethod:"+getMethod);
		if (getMethod.equals("GET")) {
			callJson = request.getParameter("data");
			log.debug("原始Get请求参数:"+callJson);
		} else {
			InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
			iny = new BufferedReader(reader);
			String line;
			while ((line = iny.readLine()) != null) {
				callJson += line;
			}
			log.debug("原始Post请求参数:"+callJson);
		}

		try {
			//解析json信息
			//JSONObject inObject = new JSONObject(callJson);
			JSONObject inObject = JSONObject.fromObject(callJson);
			log.debug("处理前的inObject：" + inObject.toString());

			String procedureName = GetJV(inObject, "procedureName");
			String inDataList = GetJV(inObject, "indata");
			int outDataNum = Integer.valueOf(GetJV(inObject,"outdata"));
			
			log.debug("调用存储过程名称Name：" + procedureName);
			log.debug("调用存储过程入参数List：" + inDataList);
			log.debug("调用存储过程出参数量Num：" + outDataNum);
			
			//JSONArray inDataArray = new JSONArray(inDataList);
			JSONArray inDataArray = inObject.getJSONArray("indata");
			int inDataLen = inDataArray.size();
			log.debug("调用存储过程入参数量Num：" + inDataLen);
			
			String inDataString = "";
			for(int i=0;i<inDataLen;i++){
				String Part = inDataArray.getString(i);
				inDataString=inDataString + ",{\"mode\":\"in\",\"type\":\"string\",\"value\":\""+Part+"\"}";
			}
			
			inDataString=inDataString.substring(1);
			
			String outDataString = "";
			for(int i=0;i<outDataNum;i++){
				outDataString=outDataString+",{\"mode\":\"out\",\"type\":\"string\",\"value\":\"\"}";
			}
			
			
			procedureData = "["+inDataString+outDataString+"]";
			
			log.debug("存储过程参数List数量：" + (inDataLen+outDataNum));
			log.debug("存储过程参数List：" + procedureData);
			String str="";
			for(int i=0;i<(inDataLen+outDataNum);i++){
			     str=str+",?";
			}
			str=str.substring(1);
			def procedure = "{call "+procedureName+" ("+str+")}";

			log.debug("调用存储过程处理SQL:" + procedure);

			list = eliteBean.executeWithMultiReturn(procedure, "MdataSource", procedureData);
			JSONArray outD = new JSONArray();
			for(int j=0;j<list.size(); j++){
				JSONObject outJ = new JSONObject();
				outJ.put("outString",list.get(j));
				outD.add(outJ);
			}
			
			log.debug("调用存储过程处理返回出参结果:" + list.toString());
			
			//outData = "{\"code\":\"1\",\"message\":\"succ\",\"data\":"+list.toString()+"}"
			outData = "{\"code\":\"1\",\"message\":\"succ\",\"data\":\"\"}"
			//JSONObject outObject = new JSONObject(outData);
		    JSONObject outObject = JSONObject.fromObject(outData);
			outObject.put("data", outD);
			outData=outObject.toString();
		}
		catch(Exception e) {
			log.debug("接口处理异常"+e.getMessage());
			//失败返回
			outData = "{\"code\":\"0\",\"message\":\""+e.getMessage()+"\"}"
			
		}
		
		log.debug("接口请求处理返回结果:" + outData);
		
		return outData;
	}
	// 解析Json值
	public static String GetJV(JSONObject inJson, String getKey) {
		String Rev_Value = null;
		try {
			Rev_Value = inJson.getString(getKey);
		} catch (Exception e) {
			try {
				Rev_Value = String.valueOf(inJson.getInt(getKey));
			} catch (Exception e1) {
				try {
					Rev_Value = String.valueOf(inJson.getJSONArray(getKey));
				} catch (Exception e2) {
					try {
						Rev_Value = String.valueOf(inJson.getJSONObject(getKey));
					} catch (Exception e3) {
						Rev_Value = "";
					}
				}
			}
		} finally {
			if (Rev_Value == null || Rev_Value.equals("null") || Rev_Value.length() <= 0) {
				Rev_Value = "";
			} else {
			}
		}
		return Rev_Value;
	}
}
